import { Applet } from "@/api/interface/iot";
import { PublicStore } from "@/stores/modules/public";
import { Dict, DictType, getDictList } from "@/utils/cacheUtil";
import { openWin } from "@/utils/routerUtil";
import { matchIp } from "@/utils/str";
import { ElMessage } from "element-plus";
import i18n from "@/languages/index";
import { IS_API_PROXY } from "@/config/config";
const { t } = i18n.global;
export const useApplet = () => {
	const appletTypeList = ref<Dict[]>([]);
	onMounted(() => {
		getAppletTypeList();
	});
	/**
	 * 跳转到应用系统
	 * @param applet
	 * @returns
	 */
	const gotoAppletPage = (applet: Applet.ResUserApplet) => {
		applet = { ...applet };
		if (!applet.home || !applet.home.startsWith("http")) {
			ElMessage.warning("当前应用不是一个Web应用");
			return;
		}

		let server = applet.home;
		// //非http地址，补充浏览器地址
		// if (!server.startsWith("http")) {
		// 	let server = import.meta.env.DEV ? "http://172.16.22.132:9000" : window.location.origin;
		// 	applet.home = server + applet.home;
		// }
		//替换应用域
		// if (import.meta.env.DEV) {
		// 	server = `http://172.16.22.132:9000`;
		// } else
		if (matchIp(server)) {
			ElMessage.warning("使用域名访问平台时生成");
			return;
		}
		// else {
		// let home = server.split("://")[1];
		// home = home.substring(home.indexOf("/"));
		//如果是域名，动态增加二级域名
		// server = `${window.location.protocol}//${applet.key}.${window.location.hostname}:${window.location.port}`;
		// console.log(server);
		// }
		let page = `${server}?token=${PublicStore().token}${applet.index ? "&redirect=" + applet.index : ""}`;
		openWin(page);
	};
	const appendToken = (url: string) => {
		let append = "";
		if (url?.includes("?") && url?.includes("=")) {
			append = `&token=${PublicStore().token}`;
		} else {
			append = `?token=${PublicStore().token}`;
		}
		return url + append;
	};
	const gotoAccess = (applet: Applet.ResUserApplet) => {
		if (IS_API_PROXY) {
			if (applet.home?.startsWith("http")) {
				openWin(appendToken(applet.home));
			} else if (applet.subdomain) {
				let url = `${applet.subdomain}`;
				if (!url.includes("/#")) {
					url = `/${url}/#/`;
				}
				openWin(appendToken(url));
			} else {
				ElMessage.warning("请设置应用主页地址");
			}
		} else {
			if (!applet.subdomain) {
				ElMessage.warning(t("index.applet_list_elmsg.subdomain"));
				return;
			}
			let page;
			if (applet.home && applet.home.startsWith("http")) {
				let index = new URL(applet.home).pathname;
				page = `${window.location.protocol}//${applet.subdomain}.${window.location.host}${index}?token=${PublicStore().token}`;
			} else {
				page = `${window.location.protocol}//${applet.subdomain}.${window.location.host}/#/?${
					applet.index ? applet.index + "&token=" + PublicStore().token : "token=" + PublicStore().token
				}`;
			}
			//pathname
			openWin(page);
		}
	};
	const getAppletTypeList = () => {
		getDictList(DictType.AppletType).then(list => {
			appletTypeList.value = list;
		});
	};
	return {
		appletTypeList,
		gotoAppletPage,
		gotoAccess
	};
};
